
function setTitle( title )
{
	$( '#title' ).text( title );
}

function ajax( params_ )
{
	var params = params_;

	if( params[ 'data' ] == null )
	{
		params[ 'data' ] = {};
	}

	params[ 'data' ][ 'sid' ] = sid; // global sid
	params[ 'data' ][ 'oid' ] = userId; // global userId
	
	$.ajax( params );		
}

function trunkStr( s, maxLen, strEnd )
{
    var strLen = s.length;
    var result;

	if( strLen < maxLen )
	{
		result = s;
	}
	else
	{
		var gap = ' ';
		var gapPos = s.lastIndexOf( gap );

		var sCopy = s;

		while( gapPos > maxLen && gapPos != -1 )
		{
			sCopy = sCopy.substring( 0, gapPos );
			gapPos = sCopy.lastIndexOf( gap );
		}

		if( gapPos != -1 )
		{
			if( gapPos < maxLen )
			{
				result = s.substring( 0, gapPos ) + strEnd;
			}
			else
			{
				result = s.substring( 0, maxLen ) + strEnd;
			}
		}
		else
		{
			result = s.substring( 0, maxLen ) + strEnd;
		}
	}

	return result;
}

function setCaretPosition(elemId, caretPos) {
    var elem = document.getElementById(elemId);

    if(elem != null) {
        if(elem.createTextRange) {
            var range = elem.createTextRange();
            range.move('character', caretPos);
            range.select();
        }
        else {
            if(elem.selectionStart) {
                elem.focus();
                elem.setSelectionRange(caretPos, caretPos);
            }
            else
                elem.focus();
        }
    }
}

function getUrlParamFromUrl( url, name )
{
	return decodeURI( (RegExp(name + '=' + '(.+?)(&|$)').exec(url)||[,null])[1] );
}

function getUrlParam(name) 
{
	return getUrlParamFromUrl( location.search );
}

function format( s )
{
	var s2 = s;
	for( var i = 1; i < arguments.length; i++ )
	{
		s2 = s2.replace( '{' + String( i - 1 ) + '}', arguments[ i ] );
	}

	return s2;
}

// The function clones an object
function clone(obj) 
{
    if (null == obj || "object" != typeof obj) return obj;
    var copy = obj.constructor();
    for (var attr in obj) {
        if (obj.hasOwnProperty(attr)) copy[attr] = obj[attr];
    }
    return copy;
}

// The function converts HTML Forms to JSON (e.g. $( '#form_id' ).serializeObject() )
$.fn.serializeObject = function()
{
    var o = {};
    var a = this.serializeArray();
    $.each(a, function() {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};